package com.meilishuo.message.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by chenhting on 15/5/8.
 */
public class MisCleint {
    protected static final String testUrl = "http://api.open.meilishuo.com/router/rest";//沙箱环境调用地址
    protected static final String appkey = "MJ205177537196";
    protected static final String secret = "15917e647bf0d56a09a8e3a42123e94e";
    public static String excuteApi(TreeMap<String,String> appParmas){
        TreeMap<String, String> apiparamsMap = new TreeMap<String, String>();
        apiparamsMap.put("format", "json");
        apiparamsMap.put("sign_method","md5");
        apiparamsMap.put("app_key",appkey);
        apiparamsMap.put("v", "1.0");
        Iterator<String> iter=appParmas.keySet().iterator();
        while (iter.hasNext()){
            String mapKey=iter.next();
            String mapValue=appParmas.get(mapKey);
            apiparamsMap.put(mapKey,mapValue);
        }
        String timestamp =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        apiparamsMap.put("timestamp",timestamp);
        //生成签名
        String sign = Util.md5Signature(apiparamsMap,secret);
        apiparamsMap.put("sign", sign);
        StringBuilder param = new StringBuilder();
        for (Iterator<Map.Entry<String, String>> it = apiparamsMap.entrySet()
                .iterator(); it.hasNext();) {
            Map.Entry<String, String> e = it.next();
            param.append("&").append(e.getKey()).append("=").append(e.getValue());
        }
        String sysParams=param.toString().substring(1);
        String result=Util.getResult(testUrl,sysParams);
        return result;
    }
}
